package 动态规划;

//712 很好的一道二维动归，并且这个状态的定义，很多地方都能用到。其次在不同字符的选择的时候
//不能直接去选择ASCII小的，这是局部贪心，验证下来是失败的，所以需要对整个成本进行比较取小，可参考例1
public class Solution4 {
    public int minimumDeleteSum(String s1, String s2) {
        int m = s1.length();
        int n = s2.length();
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            dp[i][0] = dp[i - 1][0] + s1.charAt(i - 1);
        }
        for (int j = 1; j <= n; j++) {
            dp[0][j] = dp[0][j - 1] + s2.charAt(j - 1);
        }
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
                    dp[i][j] = dp[i - 1][j - 1];
                } else {
                    int deleteS1 = s1.charAt(i - 1) + dp[i - 1][j];
                    int deleteS2 = s2.charAt(j - 1) + dp[i][j - 1];
                    dp[i][j] = Math.min(deleteS1, deleteS2);
                }
            }
        }

        return dp[m][n];
    }
}